#include "ImageDirectory.h"

ImageDirectory::ImageDirectory():ImageBase(){
    index =0;
    count = 0;

}
ImageDirectory::ImageDirectory(string path,unsigned int width,unsigned int height):ImageBase(width, height){

    ImageDirectory();
    this->path = path;
}
ImageDirectory::~ImageDirectory() {
    closeDevice();
}


int ImageDirectory::getImage(Mat &image) {   


    cout<<"["<< count <<"]"<<"["<< index <<"]" <<"read image: "<< filenames[index]<< endl;
    image = imread(filenames[index]);

    if (image.empty()) {
     cerr << "ERROR! blank frame grabbed\n";
     return -1;
    }else{
    cout<< "getImage read image success\n";
    }
    index++;
    count++;
    if (index >= filenames.size())
    {        
        index = index % filenames.size();
    }

    return 0;

    

}

int ImageDirectory::openDevice() {

    getFileNames(path, filenames);


     return 0;
}
int ImageDirectory::closeDevice() {

	
	return 0;
}

void ImageDirectory::getFileNames(string path,vector<string>& filenames)
{
    DIR *pDir;
    struct dirent* ptr;
    if(!(pDir = opendir(path.c_str()))){
        cout<<"Folder doesn't Exist!"<<endl;
        return;
    }
    while((ptr = readdir(pDir))!=0) {
        if (strcmp(ptr->d_name, ".") != 0 && strcmp(ptr->d_name, "..") != 0){
            filenames.push_back(path + "/" + ptr->d_name);
    }
    }
    closedir(pDir);
}
